package com.shiguiwu.springmybatis.designpattern.strategy.event;

import com.shiguiwu.springmybatis.designpattern.strategy.ICouponDiscount;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @description: 满减
 * @author: stone
 * @date: Created by 2021/9/9 16:52
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.strategy.event
 */
@Slf4j
public class MJCouponDiscount implements ICouponDiscount<Map<String,String>> {

    /**
     * 判断满足x 元后，减n元
     *
     * @param couponInfo 券折扣信息；直减、满减、折扣、N元购
     * @param price      sku⾦金金额
     * @return 价格
     */
    @Override
    public BigDecimal discountAmount(Map<String, String> couponInfo, BigDecimal price) {
        String x = couponInfo.get("x");
        String n = couponInfo.get("n");

        if (price.compareTo(new BigDecimal(x)) < 0) {
            return price;
        }

        BigDecimal subtract = price.subtract(new BigDecimal(n));
        //减到少于1元，直接返回1
        if (subtract.compareTo(BigDecimal.ZERO) < 1) {
            return BigDecimal.ONE;
        }

        log.info("满减优惠：满足 {}元，减去 {}，原来价格是 {}，最终价格是{}", x, n, price, subtract);
        return subtract;

    }
}
